package game

import (
	"GenshinImpact/server/src/conf"
	"fmt"
	"time"
)

type ShowRole struct {
	RoleId    int
	RoleLevel int
}

type ModPlayer struct {
	UserId         int
	Icon           int
	Card           int
	Name           string
	Sign           string
	PlayerLevel    int
	PlayerExp      int
	WorldLevel     int
	CurrWorldLevel int // 当前世界等级
	WorldLevelCool int64
	Birth          int
	HideShowTeam   int // 是否隐藏队伍展示
	ShowTeam       []*ShowRole
	ShowCard       []int

	// 未显示的
	IsProhibit int
	IsGM       int
}

func (self *ModPlayer) SetIcon(iconId int, player *Player) {
	if !player.ModIcon.IsHasIcon(iconId) {
		// 玩家没有图标 -> 通知客户端操作非法
		return
	}
	player.ModPlayer.Icon = iconId
	fmt.Println("图标已被设置：", player.ModPlayer.Icon)
}

func (self *ModPlayer) SetCard(cardId int, player *Player) {
	if !player.ModCard.IsHasCard(cardId) {
		return
	}

	player.ModPlayer.Card = cardId
	fmt.Println("名片已被设置：", player.ModPlayer.Card)
}

func (self *ModPlayer) SetName(name string, player *Player) {
	// 违禁词检测
	if GetManagerBanWord().IsBanWord(name) {
		return
	}
	player.ModPlayer.Name = name
	fmt.Println("名字已被更改:", player.ModPlayer.Name)
}

func (self *ModPlayer) SetSign(sign string, player *Player) {
	// 违禁词检测
	if GetManagerBanWord().IsBanWord(sign) {
		return
	}
	player.ModPlayer.Sign = sign
	fmt.Println("签名已被更改:", player.ModPlayer.Sign)
}

// 增加经验
func (self *ModPlayer) AddExp(exp int, player *Player) {
	self.PlayerExp += exp

	for {
		config := conf.GetCurrLevelConfig(self.PlayerLevel)
		if config == nil {
			break
		}
		if config.PlayerExp == 0 {
			break
		}
		// 是否完成必要任务（卡等级）
		if config.ChapterId > 0 && !player.ModUniqueTask.IsTaskFinished(config.ChapterId) {
			break
		}

		if self.PlayerExp >= config.PlayerExp {
			self.PlayerLevel += 1
			self.PlayerExp -= config.PlayerExp
		} else {
			break
		}
	}
	fmt.Println("当前等级：", self.PlayerLevel, "当前经验：", self.PlayerExp)
}

// 降低世界等级
func (self *ModPlayer) ReduceWorldLevel() {
	// 降低条件校验
	if self.WorldLevel < conf.REDUCE_WORLD_LEVEL_START {
		fmt.Println("降低世界等级失败:", self.WorldLevel)
		return
	}
	if self.WorldLevel-self.CurrWorldLevel >= conf.REDUCE_WORLD_LEVEL_MAX {
		fmt.Println("世界等级降低失败:", self.CurrWorldLevel)
		return
	}
	// 冷却时间校验
	if time.Now().Unix() < self.WorldLevelCool {
		fmt.Println("世界等级降低失败,处于CD")
		return
	}
	self.CurrWorldLevel -= 1
	self.WorldLevelCool = time.Now().Unix() + conf.REDUCE_WORLD_LEVEL_COOL_DOWN
	fmt.Println("世界等级修改成功")
	return
}

// 回复原来的世界等级
func (self *ModPlayer) RevertWorldLevel() {
	if self.CurrWorldLevel == self.WorldLevel {
		fmt.Println("回复世界等级失败")
		return
	}
	if time.Now().Unix() < self.WorldLevelCool {
		fmt.Println("回复失败，冷却至")
		return
	}
	self.CurrWorldLevel += 1
	self.WorldLevelCool = time.Now().Unix() + conf.REDUCE_WORLD_LEVEL_COOL_DOWN
	fmt.Println("回复世界等级成功")
	return
}

// 设置生日
func (self *ModPlayer) SetBirth(birth int, player *Player) {
	// 是否以及设置过生日
	if self.Birth > 0 {
		fmt.Println("已经设置过生日了")
		return
	}

	month := birth / 100
	day := birth % 100
	switch month {
	case 1, 3, 5, 7, 8, 10, 12:
		if day <= 0 || day > 31 {
			fmt.Println("日期设置不合法")
			return
		}
	case 4, 6, 9, 11:
		if day <= 0 || day > 30 {
			fmt.Println("日期设置不合法")
			return
		}
	case 2:
		if day <= 0 || day > 29 {
			fmt.Println("日期设置不合法")
			return
		}
	default:
		fmt.Println("日期设置不合法")
		return
	}
	self.Birth = birth
	fmt.Println("设置成功，生日为:", month, ".", day)
}

// 检测当天是否为玩家生日
func (self *ModPlayer) IsBirthDay() bool {
	month := time.Now().Month()
	day := time.Now().Day()
	if int(month) == self.Birth/100 && day == self.Birth%100 {
		return true
	}
	return false
}

// 设置角色展示名片
func (self *ModPlayer) SetShowCard(showCard []int, player *Player) {
	cardExist := make(map[int]int)
	newList := make([]int, 0)
	for _, cardId := range showCard {
		_, ok := cardExist[cardId]
		if ok {
			continue
		}
		if !player.ModCard.IsHasCard(cardId) {
			continue
		}
		newList = append(newList, cardId)
		cardExist[cardId] = 1
	}
	self.ShowCard = newList
	fmt.Println("名片内容修改成功")
}

func (self *ModPlayer) SetShowTeam(showTeam []int, player *Player) {
	roleExist := make(map[int]int)
	newList := make([]*ShowRole, 0)
	for _, roleId := range showTeam {
		_, ok := roleExist[roleId]
		if ok {
			continue
		}
		if !player.ModRole.IsHasRole(roleId) {
			continue
		}
		showRole := new(ShowRole)
		showRole.RoleId = roleId
		showRole.RoleLevel = player.ModRole.GetRoleLevel(roleId)
		newList = append(newList, showRole)
		roleExist[roleId] = 1
	}

	self.ShowTeam = newList
	fmt.Println("组队信息修改成功")
}

func (self *ModPlayer) SetHideShowTeam(hide int) {
	if hide != conf.LOGIC_FALSE && hide != conf.LOGIC_TRUE {
		return
	}
	self.HideShowTeam = hide
}
